<!DOCTYPE html>
<html style="width:100%;height:100%;">
<head lang="en">
    <meta charset="UTF-8">
    <title>微信菜单列表</title>
    <link type="text/css" rel="stylesheet" href="/resources/easyui/themes/default/easyui.css">
    <link type="text/css" rel="stylesheet" href="/resources/easyui/themes/color.css">
    <link type="text/css" rel="stylesheet" href="/resources/easyui/themes/icon.css">
    <link type="text/css" rel="stylesheet" href="/resources/css/yidong.css">
    <script type="text/javascript" src="/resources/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="/resources/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/resources/easyui/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="/resources/easyui/plugin/jquery.cookie.js"></script>
    <script type="text/javascript" src="/resources/js/Util.js"></script>
    <script type="text/javascript">
        var currentAppKey = "";
        $(document).ready(function(){
            currentAppKey = $.cookie("appId");

            if($.cookie("appId") != "woshimorendeappid123") {
                $('#comboAppId').hide();
            } else {
                $('#comboAppId').show();
            }

            $('#menuList').treegrid({
                rowStyler:function(row, index){
                    if(row && $.cookie("appId") != row.appId)
                        return 'color:gray';
                }
            });

            loadMenuList(null, {appKey:currentAppKey});
        });
        function loadMenuList(successFun, data){
            getMenu(function(data){
                $('#menuList').treegrid('loadData',data);
                if(successFun) {
                    successFun();
                }
            }, data);
        }
        function getMenu(successFun, data){
            restClient('/rest/wechat/menu/list', successFun, data);
        }
        function loadMenuCombo(defaultValue){
            var values=[];
            values.push({id:0, text:'根菜单'});
            getMenu(function(data){
                var parentId = $('#parentId');
                if(data) {
                    for(var index in data) {
                        values.push(data[index]);
                    }
                    parentId.combobox('loadData', values);
                    if(defaultValue) {
                        parentId.combobox('setValue', defaultValue);
                    } else {
                        parentId.combobox('setValue', 0);
                    }
                }
            });
        }

        function formatType(value,row,index){
            if(row.type != null) {
                if(row.type == 'click'){
                    return '点击按钮';
                } else if(row.type == 'view'){
                    return '视图按钮';
                } else if(row.type == 'location_select') {
                    return '地理位置按钮';
                } else if(row.type == 'complex') {
                    return '父按钮';
                }
            }
        }
    </script>
</head>
<body onload="isLogon();" style="width:100%;height:100%;margin: 0;">
<table id="menuList" class="easyui-treegrid" style="width:100%;height:100%;"
       toolbar="#toolbar" data-options="idField:'id',treeField:'text'"
       rownumbers="true" fitColumns="true" singleSelect="true">
    <thead>
        <tr>
            <th data-options="field:'text',width:100">菜单名</th>
            <th data-options="field:'type',width:50,formatter:formatType">类型</th>
            <th data-options="field:'key',width:30">KEY</th>
            <th data-options="field:'url',width:100">链接</th>
            <th data-options="field:'sort',width:50">顺序</th>
            <th data-options="field:'appId',width:50">APPID</th>
            <th data-options="field:'status',width:50,formatter:formatStatus">状态</th>
        </tr>
    </thead>
</table>
<div id="toolbar">
    <div id="comboAppId">
    AppId: <input class="easyui-combobox" id="appId" name="appId" style="width: 200px;"
                  data-options="valueField:'appId', textField:'description', formatter:formatAppId, editable:false">
    </div>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newOne()">新建</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editOne()">编辑</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyOne()">删除</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" plain="true" onclick="generateMenu()">发布</a>
</div>
<div id="dlg" class="easyui-dialog" style="width:400px;height:auto;padding:-20px"
     closed="true" buttons="#dlg-buttons">
    <form id="fm" class="easyui-form" method="post">
        <div class="fitem">
            <label>父菜单：</label>
            <input id="parentId" name="parentId" class="easyui-combobox easyui-validatebox"
                   data-options="valueField:'id', textField:'text', editable:false" required="true">
        </div>
        <div class="fitem">
            <label>菜单名：</label>
            <input id="name" name="name" class="easyui-textbox easyui-validatebox"
                   required="true" data-options="missingMessage:'该输入项为必输项'">
        </div>
        <div class="fitem">
            <label>类型：</label>
            <select id="type" name="type" class="easyui-combobox easyui-validatebox" data-options="editable:false" required="true">
                <option value="click">点击按钮</option>
                <option value="view">视图按钮</option>
                <option value="location_select">地理位置按钮</option>
                <option value="complex">父按钮</option>
            </select>
        </div>
        <div class="fitem" id="fmKey">
            <label>KEY：</label>
            <input id="key" name="key" class="easyui-textbox">
        </div>
        <div class="fitem" id="fmUrl">
            <label>链接：</label>
            <input id="url" name="url" class="easyui-textbox">
        </div>
        <div class="fitem">
            <label>顺序：</label>
            <input id="sort" name="sort" class="easyui-textbox easyui-validatebox"
                   required="true" data-options="missingMessage:'该输入项为必输项'">
        </div>
        <div class="fitem">
            <label>状态：</label>
            <select id="status" name="status" class="easyui-combobox easyui-validatebox" data-options="editable:false" required="true">
                <option value="0">启用</option>
                <option value="-1">禁用</option>
                <option value="-2">删除</option>
            </select>
        </div>
    </form>
</div>
<div id="dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveOne()" style="width:90px">保存</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">取消</a>
</div>
<script type="text/javascript">
    loadAppIdCombobox($('#appId'), true);
    $('#appId').combobox({
        onSelect: function(param){
            currentAppKey = param.appId;
            loadMenuList(null, {appKey: param.appId});
        }
    });

    $('#type').combobox({
        onSelect:function(record) {
            if(record.value == "click" || record.value == "location_select") {
                $('#fmKey').show();
                $('#fmUrl').hide();
            } else if(record.value == "view") {
                $('#fmKey').hide();
                $('#fmUrl').show();
            } else if(record.value == "complex") {
                $('#fmKey').hide();
                $('#fmUrl').hide();
            }
        }
    });
    var url;
    var successMsg;
    function newOne(){
        $('#dlg').dialog('open').dialog('setTitle','新建');
        $('#fm').form('clear');
        loadMenuCombo();
        $('#type').combobox('setValue', 'complex');
        $('#fmKey').hide();
        $('#fmUrl').hide();
        $('#status').combobox('setValue', 0);
        url = '/rest/wechat/menu/create';
        successMsg = "添加成功";
    }
    function editOne(){
        var row = $('#menuList').datagrid('getSelected');
        if (row) {
            if($.cookie("appId") == 'woshimorendeappid123' || $.cookie("appId") == row.appId) {
                $('#dlg').dialog('open').dialog('setTitle', '编辑');
                loadMenuCombo(row.parentId);
                $('#name').textbox('setValue', row.text);
                $('#type').combobox('setValue', row.type);
                $('#key').textbox('setValue', row.key);
                $('#url').textbox('setValue', row.url);
                $('#sort').textbox('setValue', row.sort);
                $('#status').combobox('setValue', row.status);
                url = '/rest/wechat/menu/update/' + row.id;
                successMsg = "编辑成功";
            } else {
                $.messager.show({
                    title: '温馨提醒',
                    msg: '不能编辑默认信息'
                });
            }
        }
    }
    function saveOne(){
        var data = $('#fm').serialize();
        if(currentAppKey) {
            data += "&appKey=" + currentAppKey;
        }
        restClient(url, function (result) {
            if (result.success) {
                $('#dlg').dialog('close');
                loadMenuList(function () {
                    $.messager.show({
                        title: '温馨提醒',
                        msg: successMsg
                    });
                }, {appKey:currentAppKey});
            } else {
                $.messager.show({
                    title: '温馨提醒',
                    msg: result.msg ? result.msg : result.message
                });
            }
        }, data);
    }
    function destroyOne(){
        var row = $('#menuList').datagrid('getSelected');
        if (row){
            if($.cookie("appId") == 'woshimorendeappid123' || $.cookie("appId") == row.appId) {
                var msg = '确认要删除【' + row.text + '】么?';
                if (row.parentId === 0) {
                    msg = '确认要删除【' + row.text + '】及它的子菜单么?';
                }
                $.messager.confirm('确认', msg, function (r) {
                    if (r) {
                        restClient('/rest/wechat/menu/delete/' + row.id, function (result) {
                            if (result.success) {
                                loadMenuList(function () {
                                    $.messager.show({
                                        title: '温馨提醒',
                                        msg: '删除成功'
                                    });
                                }, {appKey:currentAppKey});
                            } else {
                                $.messager.show({
                                    title: '温馨提醒',
                                    msg: result.msg ? result.msg : result.message
                                });
                            }
                        }, {appKey:currentAppKey});
                    }
                });
            } else {
                $.messager.show({
                    title: '温馨提醒',
                    msg: '不能删除默认信息'
                });
            }
        }
    }
    function generateMenu(){
        restClient('/rest/wechat/menu/generate', function (result) {
            if (result.button) {
                $.messager.show({
                    title: '温馨提醒',
                    msg: '发布成功，24小时后生效'
                });
            } else {
                $.messager.show({
                    title: '温馨提醒',
                    msg: result.msg ? result.msg : result.message
                });
            }
        }, {appKey:currentAppKey});
    }
</script>
</body>
</html>